<template>
  <div>
    <el-container class="home-container">
      <el-header>
        <div>
          <img src="../assets/logo.png" alt />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse?'64px':'200px'">
          <!-- 通过这个点击，控制menu的collapse属性进行折叠 -->
          <div class="control" @click="toggle">|||</div>
          <!-- 菜单栏 -->
          <el-menu
            :default-active="path"
            class="el-menu-vertical-demo"
            background-color="#444"
            text-color="#ccc"
            active-text-color="#4098ff"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
          >
            <!-- 一级菜单 -->
            <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/'+i0.path"
                v-for="i0 in item.children"
                :key="i0.id"
                @click="savePath('/'+i0.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{i0.authName}}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      path: ''
    }
  },
  methods: {
    logout() {
      sessionStorage.clear()
      this.$router.push('login')
    },
    toggle() {
      this.isCollapse = !this.isCollapse
    },
    savePath(p) {
      console.log(p)
      sessionStorage.setItem('path', p)
      this.path = p
    }
  },
  async mounted() {
    const { data: ret } = await this.$http.get('menus')
    console.log(ret)
    if (ret.meta.status !== 200) return this.$message.error(ret.meta.msg)
    this.menuList = ret.data
  },
  created() {
    // 刷新后取index赋值给menu的default-active属性
    this.path = sessionStorage.getItem('path')
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-aside {
  background-color: #444;
  color: #ccc;
  width: 200px;
  height: 100%;
  box-sizing: border-box;
  .control {
    letter-spacing: 3px;
    background-color: grey;
    text-align: center;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding-bottom: 2px;
  }
  .el-menu {
    background-color: #444;
    border: none;
    .iconfont {
      margin-right: 10px;
    }
    .el-sub-menu {
      .el-menu-item {
        margin-left: 10px;
      }
    }
  }
}

.el-main {
  background-color: #e9eef3;
  color: #333;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #444;
  color: #ccc;
  text-align: center;
  height: 10%;
  line-height: 100%;
  img {
    height: 30px;
    vertical-align: middle;
  }
  span {
    font-size: 24px;
    padding-left: 10px;
  }
  .el-button {
    height: 60%;
  }
}
</style>
